//
//  XSJZZJJSystemSettingViewController.m
//  JJHome
//
//  Created by Meng Chen on 16/8/17.
//  Copyright © 2016年 LeeRay. All rights reserved.
//

#import "XSJZZJJSystemSettingViewController.h"

#import "XBConst.h"
#import "XSJZZXBSettingCell.h"
#import "XSJZZXBSettingItemModel.h"
#import "XSJZZXBSettingSectionModel.h"

#import "XSJZZCacheHelper2.h"
#import "XSJZZJJFeedBackViewController.h"
#import "XSJZZJJWebViewController.h"
#import "XSJZZJJAboutUsViewController.h"
//#import "Factory.h"

@interface XSJZZJJSystemSettingViewController ()
@property (nonatomic,strong) NSArray  *sectionArray; /**< section模型数组*/
@property (nonatomic ,strong)XSJZZXBSettingItemModel *item1;
@property (nonatomic ,strong)XSJZZXBSettingItemModel *item2;
@property (nonatomic ,strong)XSJZZXBSettingItemModel *item3;
@property (nonatomic ,strong)XSJZZXBSettingItemModel *item4;
@end

@implementation XSJZZJJSystemSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"设置";
    
//    [Factory addMenuItemToVC:self];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    label.text = @"   -new world，new life";
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    
    
   
    //设置不显示多余的空cell
    self.tableView.tableFooterView = label;
    [self setupSections];
}
- (void)setupSections
{
    
    //************************************section1
    self.item1 = [[XSJZZXBSettingItemModel alloc]init];
    _item1.funcName = @"推送提醒";
    __weak XSJZZJJSystemSettingViewController *weakself = self;
    _item1.switchValueChanged = ^(BOOL isOn){
#warning mark  此处的推送设置 
       NSLog(@"推送提醒开关状态===%@",isOn?@"open":@"close");
        
    };
    _item1.accessoryType = XBSettingAccessoryTypeSwitch;
    
    
    
    self.item2 = [[XSJZZXBSettingItemModel alloc]init];
    _item2.funcName = @"清理缓存";
    CGFloat CachSize = [XSJZZCacheHelper2 folderSizeAtPath];
    NSString * cachMessage = [NSString stringWithFormat:@"%.2fMB",CachSize];
    _item2.detailText = cachMessage;
    _item2.accessoryType = XBSettingAccessoryTypeDisclosureIndicator;
    _item2.executeCode = ^{
      
        [XSJZZCacheHelper2 cleanCache:^{
            
#warning Mark 此处直接显示清理缓存成功不合适  需要有一个清理缓存的进度
            [SVProgressHUD showInfoWithStatus:@"清理缓存成功"];
            weakself.item2.detailText = @"0.00MB";
            //此处需要刷新tableview的cell  使cell的值进行更改
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
       
    };
    
    self.item3 = [[XSJZZXBSettingItemModel alloc]init];
    _item3.funcName = @"反馈";
    _item3.accessoryType = XBSettingAccessoryTypeDisclosureIndicator;
    _item3.executeCode = ^{
        XSJZZJJFeedBackViewController *feedBackVc = [[XSJZZJJFeedBackViewController alloc]init];
        feedBackVc.hidesBottomBarWhenPushed = YES;
        [weakself.navigationController pushViewController:feedBackVc animated:YES];
    };
    
    XSJZZXBSettingSectionModel *section1 = [[XSJZZXBSettingSectionModel alloc]init];
    section1.sectionHeaderHeight = 25;
    section1.itemArray = @[_item1,_item2,_item3];
    
//    self.item4 = [[XSJZZXBSettingItemModel alloc]init];
//    _item4.funcName = @"关于我们";
//
//    _item4.accessoryType = XBSettingAccessoryTypeDisclosureIndicator;
//    _item4.executeCode = ^{
//        XSJZZJJAboutUsViewController *aboutUsVc = [[XSJZZJJAboutUsViewController alloc]init];
//        [weakself.navigationController pushViewController:aboutUsVc animated:YES];
//    };
    
    
    
    XSJZZXBSettingItemModel *item5 = [[XSJZZXBSettingItemModel alloc]init];
    item5.funcName = @"免责声明";
    item5.executeCode = ^{
        XSJZZJJWebViewController *protrocl = [[XSJZZJJWebViewController alloc]init];
        protrocl.hidesBottomBarWhenPushed = YES;
        [weakself.navigationController pushViewController:protrocl animated:YES];
    };
    
    item5.accessoryType = XBSettingAccessoryTypeDisclosureIndicator;
    
    XSJZZXBSettingSectionModel *section2 = [[XSJZZXBSettingSectionModel alloc]init];
    section2.sectionHeaderHeight = 18;
    section2.itemArray = @[item5];
    
    
    
    
    self.sectionArray = @[section1,section2];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    XSJZZXBSettingSectionModel *sectionModel = self.sectionArray[section];
    return sectionModel.itemArray.count;}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"setting";
    XSJZZXBSettingSectionModel *sectionModel = self.sectionArray[indexPath.section];
    XSJZZXBSettingItemModel *itemModel = sectionModel.itemArray[indexPath.row];
    
    XSJZZXBSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[XSJZZXBSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.item = itemModel;
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    XSJZZXBSettingSectionModel *sectionModel = self.sectionArray[section];
    return sectionModel.sectionHeaderHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XSJZZXBSettingSectionModel *sectionModel = self.sectionArray[indexPath.section];
    XSJZZXBSettingItemModel *itemModel = sectionModel.itemArray[indexPath.row];
    if (itemModel.executeCode) {
        itemModel.executeCode();
    }
}

#warning mark 搞不懂你这段代码是搞鸡毛的
//uitableview处理section的不悬浮，禁止section停留的方法，主要是这段代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    XSJZZXBSettingSectionModel *sectionModel = [self.sectionArray firstObject];
    CGFloat sectionHeaderHeight = sectionModel.sectionHeaderHeight;
    
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
